﻿using System.Net.Http;
using AutoMapper;
using Microsoft.EntityFrameworkCore;
using Microsoft.Extensions.Configuration;
using Microsoft.Extensions.DependencyInjection;
using WeChat.BaseInterface;
using WeChat.Dto;
using WeChat.EntityFramework;

namespace WeChat.UI
{
    /// <summary>
    /// 初始化注入
    /// </summary>
    public static class WeChatInit
    {
        /// <summary>
        /// 初始化注入
        /// </summary>
        public static void WeChatRegisterInit(this IServiceCollection services, IConfiguration configuration)
        {
            //todo:ef 3.0时 使用以下即可 
            //services.AddDbContext<DbContext, WeChatContext>(options =>
            //{
            //    //SqlServer 2008 必需用这个 UseRowNumberForPaging 否则无法分页
            //    //参考 https://www.cnblogs.com/Jackyye/p/9359147.html
            //    // EF Core 3.0 不支持SqlServer2008 分页
            //    //https://github.com/aspnet/EntityFrameworkCore/issues/16400 

            //    //https://dotnet.microsoft.com/download/dotnet-core/3.0  preview5应该支持  后期换了数据库在升级为正式
            //    // options.UseSqlServer(connectionStr, a => a.UseRowNumberForPaging());

            //    options.UseSqlServer(connectionStr);
            //});

            //AutoMapper注入
            //.net core 3.0 最新用法
            //https://www.codementor.io/zedotech/how-to-using-automapper-on-asp-net-core-3-0-via-dependencyinjection-zq497lzsq
            services.AddAutoMapper(typeof(WeChatMapperInit));

            //注入HttpClient
            services.AddHttpClient("WeChatDisableCookies")
                .ConfigurePrimaryHttpMessageHandler(() => new SocketsHttpHandler()
                {
                    //取消自动跳转
                    AllowAutoRedirect = false,
                    //不自动设置cookie
                    UseCookies = false
                });

        }
    }
}
